﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Process.BLL;
using Process.Entities;
using KZ.Utilities;
using System.Text.RegularExpressions;
using Process;

namespace Business.Administrator
{
    public partial class Order : System.Web.UI.Page
    {
        Regex regex = new Regex(@"(.*)(\?op\=)(.*)");

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadProvider();
                LoadOrderDetail();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadOrderDetail();
        }

        protected void btnUpdateStatus_Click(object sender, EventArgs e)
        {
            string transID = String.Empty;
            string orderID = String.Empty;
            string providerID = String.Empty;
            CheckBox chk = new CheckBox();
            foreach (GridViewRow row in dtgOrder.Rows)
            {
                chk = row.FindControl("chkSelect") as CheckBox;
                if (chk != null && chk.Checked)
                {
                    orderID = Utility.StringParse(dtgOrder.DataKeys[row.RowIndex].Value);
                    transID = (row.FindControl("hidTransID") as HiddenField).Value;
                    providerID = (row.FindControl("hidProviderID") as HiddenField).Value;

                    break;
                }
            }
            if (!String.IsNullOrEmpty(transID) && !String.IsNullOrEmpty(orderID) && !String.IsNullOrEmpty(providerID))
            {
                ProviderENT provider = ProviderBLL.GetProviderByID(providerID);
                ConfirmUpdateInventory(provider.WS5, orderID, transID);
            }
        }

        private void LoadProvider()
        {
            ddlProvider.DataSource = ProviderBLL.GetProviderListForCMB();
            ddlProvider.DataBind();
        }

        private void LoadOrderDetail()
        {
            List<OrderDetailENT> orderDetailList = InventoryBLL.GetUpdateInventoryNotYetDone(ddlProvider.SelectedValue);
            dtgOrder.DataSource = orderDetailList;
            dtgOrder.DataBind();

            btnUpdateStatus.Visible = orderDetailList.Count > 0;
        }

        private void ConfirmUpdateInventory(string wsLink, string orderID, string transID)
        {
            string ws = regex.Replace(wsLink, "$1");
            if (!ws.EndsWith("?wsdl")) { ws += "?wsdl"; }
            WebServiceInvoker invoker = new WebServiceInvoker(new Uri(ws));

            string methodName = regex.Replace(wsLink, "$3");
            OrderBLL orderBLL = new OrderBLL();

            object[] obj = { Global.ProviderSID, transID, String.Empty };
            string result = invoker.InvokeMethod<string>(invoker.AvailableServices[0], methodName, obj);
            if (result == "0")
            {
                orderBLL.UpdateOrderStatus(orderID, OrderStatus.Done);
                lblMessage.Text = "Cập nhật nhập kho thành công.";
                message.Attributes.Add("class", "notification success png_bg message");
            }
            else
            {
                lblMessage.Text = "Cập nhật nhập kho không thành công.";
                message.Attributes.Add("class", "notification error png_bg message");
            }
            message.Style["display"] = "block";
        }
    }
}